const express = require('express');
const router = express.Router();
const axios = require('axios');

const GPT_URL = 'https://spark-api-open.xf-yun.com/v1/chat/completions'; // 确保这个 URL 正确
const header = {
  Authorization: 'Bearer pAuRkiQnNvZPMSXhUsrF:xXXpqIlAlklLsyhzjdFh',
};

const parameter = {
  max_tokens: 4096,
  top_k: 4,
  temperature: 0.5,
  messages: [
    {
      role: "system",
      content: "你现在是一名专业养老院管理专家,你处理过养老院的各种事务，能做出精准的指导；接下来请用养老院管理专家的口吻和用户对话。自动生成问题描述，减少用户手动输入的工作量。根据事件内容生成推荐语，帮助用户更好地做出反应。",
    },
    {
      role: "user",
      content: "请在此处输入你的问题!!!",
    },
  ],
  model: "4.0Ultra",
  // 移除流式响应配置
  stream: false,
};

router.post('/message', async (req, res) => {
  const { message } = req.body;
  console.log('Received from client:', message);

  // 更新请求参数
  parameter.messages[1].content = message;

  try {
    // 发送请求并等待响应
    const response = await axios.post(GPT_URL, parameter, { headers: header });

    // 直接返回AI回复
    const data = response.data;
    console.log('AI Response:', data);

    // 将完整的响应返回给客户端
    res.json(data);

  } catch (error) {
    console.error('Error during HTTP request:', error);
    res.status(500).json({ message: 'Error during HTTP request.' });
  }
});

module.exports = router;
